import { constants } from './index';
import axios from 'axios'
import { fromJS } from 'immutable'

const changeHomeData = (result) => ({
  type: constants.CHANGE_HOME_DATA,
  topicList: result.topicList,
  articleList: result.articleList,
  recommdPic: result.recommdPic,
  recommdList: result.recommdList
})

const addHomeList = (result,nextPage) => ({
  type: constants.ADD_ARTICLE_LIST,
  list: fromJS(result),
  nextPage
})

export const downloadShow = () => ({
  type: constants.DOWNLOAD_SHOW
});

export const downloadHide = () => ({
  type: constants.DOWNLOAD_HIDE
});

export const getHomeInfo = () => {
  return (dispatch) => {
    axios.get('/api/home.json').then(res => {
      const result = res.data.data
      dispatch(changeHomeData(result))
    }).catch(err=>{
      console.log(err)
    })
  }
};

export const getMoreList = (page) => {
  return (dispatch) => {
    axios.get('/api/homeList.json?page=' + page).then(res => {
      const result = res.data.data
      dispatch(addHomeList(result, page + 1))
    })
  }
};

export const toggleTopShow = (show) => ({
  type: constants.TOGGLE_TOP_SHOW,
  show
})
